<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>API to TypeScript Type Converter</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .input-group {
            margin-bottom: 20px;
        }
        input[type="text"] {
            width: 100%;
            padding: 8px;
            margin-bottom: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        textarea {
            width: 100%;
            height: 200px;
            padding: 8px;
            margin-bottom: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-family: monospace;
        }
        .button-group {
            margin-bottom: 20px;
        }
        button {
            padding: 8px 16px;
            margin-right: 10px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            background-color: #4CAF50;
            color: white;
        }
        button:hover {
            background-color: #45a049;
        }
        .history {
            margin-top: 20px;
        }
        .history-item {
            margin-bottom: 10px;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            background-color: #f9f9f9;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>API to TypeScript Type Converter</h1>
        
        <div class="input-group">
            <input type="text" id="apiUrl" placeholder="输入API地址">
            <div class="button-group">
                <button onclick="convertApi()">转换</button>
                <button onclick="copyOutput()">复制</button>
                <button onclick="clearOutput()">清空</button>
            </div>
            <textarea id="output" readonly placeholder="TypeScript类型定义将在这里显示"></textarea>
        </div>

        <div class="history">
            <h2>历史记录</h2>
            <button onclick="clearHistory()">清空历史记录</button>
            <div id="historyList"></div>
        </div>
    </div>

    <script>
        // 转换API
        async function convertApi() {
            const apiUrl = document.getElementById('apiUrl').value;
            if (!apiUrl) {
                console.log('请输入API地址');
                return;
            }

            try {
                const response = await fetch('/convert', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ api_url: apiUrl })
                });
                const data = await response.json();
                
                if (data.success) {
                    document.getElementById('output').value = data.ts_type;
                    loadHistory();
                } else {
                    console.log('转换失败: ' + data.error);
                }
            } catch (error) {
                console.log('请求失败: ' + error);
            }
        }

        // 复制输出内容
        function copyOutput() {
            const output = document.getElementById('output');
            output.select();
            document.execCommand('copy');
        }

        // 清空输出
        function clearOutput() {
            document.getElementById('apiUrl').value = '';
            document.getElementById('output').value = '';
        }

        // 加载历史记录
        async function loadHistory() {
            try {
                const response = await fetch('/history');
                const history = await response.json();
                
                const historyList = document.getElementById('historyList');
                historyList.innerHTML = history.map(item => `
                    <div class="history-item">
                        <div>API: ${item.api_url}</div>
                        <div>时间: ${item.created_at}</div>
                        <button onclick='loadHistoryItem(${JSON.stringify(item)})'>加载</button>
                    </div>
                `).join('');
            } catch (error) {
                console.error('加载历史记录失败:', error);
            }
        }

        // 加载历史记录项
        function loadHistoryItem(item) {
            document.getElementById('apiUrl').value = item.api_url;
            document.getElementById('output').value = item.ts_type;
        }

        // 清空历史记录
        async function clearHistory() {
            if (!confirm('确定要清空所有历史记录吗？')) {
                return;
            }

            try {
                await fetch('/clear-history', { method: 'POST' });
                loadHistory();
            } catch (error) {
                console.log('清空历史记录失败: ' + error);
            }
        }

        // 页面加载时获取历史记录
        window.onload = loadHistory;
    </script>
</body>
</html>